{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-7f3d09f0d0ed>:22: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From C:\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From C:\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting F:\\AI and Python\\人工智能学习资料\\week7\\input_data\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting F:\\AI and Python\\人工智能学习资料\\week7\\input_data\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting F:\\AI and Python\\人工智能学习资料\\week7\\input_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting F:\\AI and Python\\人工智能学习资料\\week7\\input_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "import numpy as np\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "#import tensorlayer as tl\n",
    "'''from keras.layers.core import Dense, Flatten\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras.layers.pooling import MaxPooling2D\n",
    "\n",
    "from keras import backend as K\n",
    "K.image_data_format()'''\n",
    "\n",
    "FLAGS = None\n",
    "# Import data\n",
    "data_dir = 'F:\\AI and Python\\人工智能学习资料\\week7\\input_data'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)\n",
    "# 占位\n",
    "x = tf.placeholder(tf.float32, shape=[None, 784], name='x')\n",
    "y_ = tf.placeholder(tf.int64, shape = [None,10 ], name='y_')\n",
    "learning_rate=tf.placeholder(tf.float32)\n",
    "with tf.name_scope('reshape'):\n",
    "    input_x=tf.reshape(x,[-1, 28, 28, 1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 建立模型\n",
    "with tf.name_scope('conv1'):\n",
    "    h_conv1=tf.layers.conv2d(input_x,32,[5,5],padding='SAME',activation=tf.nn.relu)\n",
    "    collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS']\n",
    "with tf.name_scope('pool1'):\n",
    "    h_pool1=tf.layers.max_pooling2d(h_conv1,pool_size=[2,2],strides=[2,2],padding=\"VALID\")\n",
    "with tf.name_scope('conv2'):\n",
    "    h_conv2=tf.layers.conv2d(h_pool1,64,[5,5],padding='SAME',activation=tf.nn.relu)\n",
    "    collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS']\n",
    "with tf.name_scope('pool2'):\n",
    "    h_pool2=tf.layers.max_pooling2d(h_conv2,pool_size=[2,2],strides=[2,2],padding=\"VALID\")\n",
    "with tf.name_scope('fc1'):\n",
    "    h_pool2_flat=tf.layers.flatten(h_pool2)\n",
    "    h_fc1=tf.layers.dense(h_pool2_flat,1024,activation=tf.nn.relu)\n",
    "    collections=[tf.GraphKeys.GLOBAL_VARIABLES,'WEIGHTS']\n",
    "with tf.name_scope('dropout'):\n",
    "    keep_prob=tf.placeholder(tf.float32)\n",
    "    h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)\n",
    "with tf.name_scope('fc2'):\n",
    "    y=tf.layers.dense(h_fc1_drop,10,activation=None)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义损失函数\n",
    "cross_entropy=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_,logits=y))\n",
    "#l2_loss=tf.add_n([tf.nn.l2_loss(w) for w in tf.get_collection('WEIGHTS')])\n",
    "#total_loss=cross_entropy+7e-5*l2_loss \n",
    "# 定义优化器\n",
    "#train_param = network.all_params\n",
    "#train_op = tf.train.AdamOptimizer(learning_rate=0.0001, use_locking=False).minimize(cost, var_list=train_param)\n",
    "#train_op = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cost)\n",
    "train_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sess=tf.Session()\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op) \n",
    "for i in range(2000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    lr=0.3\n",
    "    _,cross_entropy_v=sess.run([train_step,cross_entropy], feed_dict={x: batch_xs, y_: batch_ys,learning_rate:lr,keep_prob:0.5})\n",
    "    #sess.run([train_step,cross_entropy], feed_dict={x: batch_xs, y_: batch_ys,learning_rate:lr,keep_prob:0.5})\n",
    "    if (i+1)%100 == 0:\n",
    "        # estimate accuarcy\n",
    "        correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "        print('step %d, training accuarcy %g'%(i+1, sess.run(accuracy,feed_dict={x: batch_xs, y_: batch_ys,keep_prob:1.0})))\n",
    "        print(\"test accuracy %g\"%(sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0})))\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
